@page "/admin/posts/create"
@page "/admin/posts/edit/{id:int}"

@using MyBlogServer.Models
@using MyBlogServer.Services
@inject PostService PostService
@inject NavigationManager Navigation

@layout AdminLayout
@rendermode InteractiveServer

<PageTitle>Admin - Edit Post</PageTitle>

<AdminRouteTemplate>
    <div class="admin-container">
        <h2>@(IsEditMode ? "编辑文章" : "新建文章")</h2>
        
        <EditForm Model="@_post" OnValidSubmit="HandleValidSubmit">
            <DataAnnotationsValidator />
            <ValidationSummary />
            
            <div class="mb-3">
                <label for="title" class="form-label">标题</label>
                <InputText id="title" class="form-control" @bind-Value="_post.Title" />
            </div>
            
            <div class="mb-3">
                <label for="slug" class="form-label">Slug</label>
                <InputText id="slug" class="form-control" @bind-Value="_post.Slug" />
            </div>
            
            <!-- 添加发布日期输入 -->
            <div class="mb-3">
                <label for="createdAt" class="form-label">发布日期</label>
                <InputDate id="createdAt" class="form-control" @bind-Value="_post.CreatedAt" />
            </div>
            
            <div class="mb-3">
                <label for="tags" class="form-label">标签 (用逗号分隔)</label>
                <InputText id="tags" class="form-control" @bind-Value="_post.Tags" />
            </div>
            
            <!-- 添加阅读数输入 -->
            <div class="mb-3">
                <label for="viewCount" class="form-label">阅读数</label>
                <InputNumber id="viewCount" class="form-control" @bind-Value="_post.ViewCount" />
            </div>
            
            <div class="mb-3">
                <label for="summary" class="form-label">摘要</label>
                <textarea id="summary" class="form-control" @bind="_post.Summary" rows="3"></textarea>
            </div>
            
            <div class="mb-3">
                <label for="content" class="form-label">内容 (Markdown)</label>
                <textarea id="content" class="form-control" @bind="_post.Content" rows="15"></textarea>
            </div>
            
            <div class="mb-3 form-check">
                <InputCheckbox id="published" class="form-check-input" @bind-Value="_post.Published" />
                <label class="form-check-label" for="published">发布</label>
            </div>
            
            <div class="mb-3">
                <button type="submit" class="btn btn-primary">保存</button>
                <button type="button" class="btn btn-secondary ms-2" @onclick="Cancel">取消</button>
            </div>
        </EditForm>
    </div>
</AdminRouteTemplate>

@code {
    [Parameter] public int? id { get; set; }
    
    private Post _post = new();
    private bool IsEditMode => id.HasValue;

    protected override async Task OnInitializedAsync()
    {
        if (IsEditMode)
        {
            var post = await PostService.GetPostByIdAsync(id.Value);
            if (post != null)
            {
                _post = post;
            }
        }
    }

    private async Task HandleValidSubmit()
    {
        try
        {
            if (IsEditMode)
            {
                await PostService.UpdatePostAsync(_post);
            }
            else
            {
                await PostService.CreatePostAsync(_post);
            }
            
            Navigation.NavigateTo("/admin/posts");
        }
        catch (Exception ex)
        {
            // Handle error
            Console.WriteLine($"保存文章时出错: {ex.Message}");
        }
    }

    private void Cancel()
    {
        Navigation.NavigateTo("/admin/posts");
    }
}